//
//  AppGlobalCenter.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/11/7.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "AppGlobalCenter.h"

@interface AppGlobalCenter ()
@property (nonatomic, copy) NSString *inviteCode;
@property (nonatomic, copy) NSString *codeUrl;
@property (nonatomic, copy) UIImage *inviteCodeImage;
@end
@implementation AppGlobalCenter
+ (AppGlobalCenter *)share {
    static AppGlobalCenter *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareInstance = [[AppGlobalCenter alloc]init];
    });
    return shareInstance;
}

- (void)preRequestMyQr:(void (^)(NSString * _Nullable, UIImage * _Nullable, NSError * _Nullable))complete {
    if ([TTUserModel sharedObject].token.length == 0) {
        return;
    }
    if (self.inviteCodeImage != nil) {
        !complete ?: complete(self.inviteCode, self.inviteCodeImage, nil);
        return;
    }
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_MyCode];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    !complete ?: complete(nil, nil, [NSError errorWithDomain:API_MyCode code:0 userInfo:nil]);
                    return;
                }
                weakSelf.inviteCode = [resultDic valueForKey:@"code"];
                weakSelf.codeUrl = [resultDic valueForKey:@"codeUrl"];
                [weakSelf fillViewWithData];
                !complete ?: complete(weakSelf.inviteCode, weakSelf.inviteCodeImage, nil);
            } else {
                NSString * message = [data valueForKey:@"message"];
                [SVProgressHUD showErrorWithStatus:message];
                [SVProgressHUD dismissWithDelay:1];
                !complete ?: complete(nil, nil, [NSError errorWithDomain:API_MyCode code:0 userInfo:nil]);
            }
        } else {
            !complete ?: complete(nil, nil, [NSError errorWithDomain:API_MyCode code:0 userInfo:nil]);
        }
    } FailedBlock:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, nil, error);
    }];
}
- (void)fillViewWithData {
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSString *string = self.codeUrl; //加入二维码url
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    CIImage *image = [filter outputImage];
    CGAffineTransform transform = CGAffineTransformMakeScale(10.0f, 10.0f);
    CIImage *output = [image imageByApplyingTransform: transform];
    UIImage *newImage = [UIImage imageWithCIImage:output scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
    self.inviteCodeImage = newImage;
}
@end
